package com.cspg.design.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.cspg.design.R;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.presenter.HomeFragmentPresenter;
import com.cspg.design.ui.gtasks.GTasksActivity;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.ui.search.SearchActivity;
import com.cspg.design.utils.StatusBarUtil;

import java.io.Serializable;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class HomeFragment extends Fragment {

    private HomeFragmentPresenter homeFragmentPresenter;
    private Badge badge;

    public View onCreateView(@NonNull final LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        StatusBarUtil.setStatusBarColor(getActivity(),R.color.cspgPrimary);

        StatusBarUtil.setLightStatusBar(getActivity(),false,false);

        Button mBtnWaitWork = (Button) root.findViewById(R.id.wait_work);
        Button mBtnDesign = (Button) root.findViewById(R.id.project_design);
        badge = new QBadgeView(getContext()).bindTarget(mBtnWaitWork).setBadgeGravity(Gravity.END | Gravity.TOP);
        homeFragmentPresenter = new HomeFragmentPresenter(getActivity(),badge);

        initData();
        mBtnWaitWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GTasksActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("project", (Serializable) homeFragmentPresenter.getProjectDtos());
                intent.putExtras((bundle) );
                startActivity(intent);
            }
        });
        mBtnDesign.setOnClickListener( (v) ->{
                    Intent intent = new Intent(getContext(), SearchActivity.class);
                    intent.putExtra("type",2);
                    startActivity(intent);
                }
        );

        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    private void initData()
    {
        if (UserInfo.getInstance() == null || UserInfo.getInstance().EmployeeID == null)
        {
            startActivity(new Intent(getActivity(),LoginActivity.class));
        }
        else
        {
            homeFragmentPresenter.project();
        }

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}